package cn.com.oceansoft.business.szjf.controller;

import cn.com.oceansoft.business.base.controller.BaseController;
import cn.com.oceansoft.business.base.entity.Pager;
import cn.com.oceansoft.business.base.entity.Query;
import cn.com.oceansoft.business.base.entity.Result;
import cn.com.oceansoft.business.szjf.entity.*;
import cn.com.oceansoft.business.szjf.service.IPsshService;
import cn.com.oceansoft.business.szjf.service.IYsshService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/6/26.
 * 验收资料控制器
 */
@Controller
@RequestMapping("yssh")
public class YsshController extends BaseController {
    @Resource
    private IYsshService ysshService;
    @Resource
    private IPsshService psshService;
    /**
     * 进入验收审核列表页面
     * @param model
     * @return
     */
    @RequestMapping(value = "index")
    public String gotoYssh(Model model){

        List<Regional> list = psshService.getRegional();
        model.addAttribute("regional",list);
        return "szjf/yssh/yssh";
    }
    /**
     * 进入抽取专家页面
     * @param model
     * @return
     */
    @RequestMapping(value = "gotoCqzj/{id}")
    public String gotoCqzj(Model model,@PathVariable("id") String id){

//        List<Regional> list = psshService.getRegional();
//        model.addAttribute("regional",list);
        model.addAttribute("ProjectGuid",id);
        return "szjf/yssh/cqzj";
    }

    /**
     *获取验收初审数据
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getYsch")
    public Result getYsch(Query query, Yssh yssh){
        yssh.setProjectstatus("0");//初审
        if(!"0".equals(getSession().getPositionlevel())){//如果是管理员不需要限制权限
            yssh.setPositionid(getSession().getPositionguid());//权限
        }
        query.setObj(yssh);
        Pager<Yssh> pager = ysshService.findYsshByPage(query);
        return new Result(Result.SUCC,pager);
    }
    /**
     *获取验收受理数据
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getYssl")
    public Result getYssl(Query query, Yssh yssh){
        yssh.setProjectstatus("1");//受理中
        if(!"0".equals(getSession().getPositionlevel())){//如果是管理员不需要限制权限
            yssh.setPositionid(getSession().getPositionguid());//权限
        }
        query.setObj(yssh);
        Pager<Yssh> pager = ysshService.findYsshByPage(query);
        return new Result(Result.SUCC,pager);
    }

    /**
     *获取抽取专家数据
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getCqzj")
    public Result getCqzj(Query query, Yssh yssh){
        yssh.setProjectstatus("2");//验收中-尚未抽取专家
        if(!"0".equals(getSession().getPositionlevel())){//如果是管理员不需要限制权限
            yssh.setPositionid(getSession().getPositionguid());//权限
        }
        query.setObj(yssh);
        Pager<Yssh> pager = ysshService.findYsshByPage(query);
        return new Result(Result.SUCC,pager);
    }

    /**
     * 通过验收初审
     * @param yssh
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "yschApproval")
    public Result yschApproval(Yssh yssh){
        try {
            yssh.setProjectstatus("1");//变成受理中
            yssh.setProjectstatus_cn("受理中");

            Yssh s = ysshService.getObjByGuid(yssh.getGuid());
            Logmanage log = new Logmanage();
            log.setUserguid(getSession().getGuid());
            log.setLogmodule("验收资料初审");
            log.setLogpage("_CsPass");
            log.setLogtype("记录日志");
            log.setLogcontent(s.getProjectno()+"项目 验收资料初审 已通过");
            log.setOperationtme(new Date());
            log.setIsdelete("0");
            CheckInfo info = new CheckInfo();
            info.setPaguid(yssh.getGuid());
            info.setCheckresult("通过");
            info.setCheckstep("验收初审");
            info.setComments("初审通过，流程至验收验收资料受理");
            info.setCheckuser(getSession().getGuid());//当前用户guid
            info.setCheckdate(new Date());
            info.setReturnnode("");
            ysshService.updateStatusByGuid(log,yssh,info);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(Result.FAILURE,"操作失败");
        }
        return  new Result(Result.SUCC,"操作成功");
    }

    /**
     * 通过验收受理
     * @param yssh
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "ysslApproval")
    public Result ysslApproval(Yssh yssh){
        try {
            yssh.setProjectstatus("2");//变成受理中
            yssh.setProjectstatus_cn("验收中");

            Yssh s = ysshService.getObjByGuid(yssh.getGuid());
            Logmanage log = new Logmanage();
            log.setUserguid(getSession().getGuid());
            log.setLogmodule("验收受理");
            log.setLogpage("_SlPass");
            log.setLogtype("记录日志");
            log.setLogcontent(s.getProjectno()+"项目 验收资料受理  已通过");
            log.setOperationtme(new Date());
            log.setIsdelete("0");
            CheckInfo info = new CheckInfo();
            info.setPaguid(yssh.getGuid());
            info.setCheckresult("通过");
            info.setCheckstep("验收受理");
            info.setComments("受理通过，流程至抽取验收专家");
            info.setCheckuser(getSession().getGuid());//当前用户guid
            info.setCheckdate(new Date());
            info.setReturnnode("");
            ysshService.updateStatusByGuid(log,yssh, info);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(Result.FAILURE,"操作失败");
        }
        return  new Result(Result.SUCC,"操作成功");
    }
    /**
     * 根据project的id获取详细信息
     * @param model
     * @param guid
     * @return
     */
    @RequestMapping("getDetail/{guid}")
    public String getDetail(Model model,@PathVariable("guid")String guid){
        Pssh item = psshService.getDetailByPrjNo(guid);
        model.addAttribute("detail",item);
        return "szjf/yssh/detail";
    }

}
